
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author personal
 */
public class TitleScreen extends GameCanvas implements Runnable {

    private MobileMonstersMIDlet midlet;
    //private AnimatedSprite spBalloon;
    //private LayerManager lmgr;
    //private String pathBalloon = "";
    private String pathAlthur = "";
    //private Image imgTitle;
    private boolean running = false;
    private int loop = 0;
    private Thread thread = null;
    //speed
    private int speedGame = 40;
    //disable and paused
    //boolean
    private boolean isPaused = false;
    //button
    private ButtonImage btnResume;
    //image
    private Image imgBtnResume;
    private String pathBtnResume = "";

    public TitleScreen(MobileMonstersMIDlet midlet) {
        super(true);
        this.midlet = midlet;
        setFullScreenMode(true);

        pathAlthur = "/althur.png";
        pathBtnResume = "/btnResume320.png";

        try {
            //imgTitle = Image.createImage(pathAlthur);
            imgBtnResume = Image.createImage(pathBtnResume);

            int xyArea[] = new int[4];

            int xResume = getWidth() - imgBtnResume.getWidth();
            int yResume = getHeight() - imgBtnResume.getHeight();
            xyArea[0] = xResume; //x1
            xyArea[1] = xResume + imgBtnResume.getWidth(); //x2
            xyArea[2] = yResume; //y1
            xyArea[3] = yResume + imgBtnResume.getHeight(); //y2
            btnResume = new ButtonImage("resume", imgBtnResume, imgBtnResume,
                    imgBtnResume, xResume,
                    yResume, xyArea, Graphics.TOP | Graphics.LEFT);

            //animated sprite

            //lmgr = new LayerManager();
            //lmgr.append(spBalloon);
        } catch (IOException ex) {
        }
        //new Thread(this).start();
        //setFullScreenMode(true);

        
        //updatePositions();
    }

    public void start() {
        if (!isPaused) {
            running = true;
            thread = new Thread(this);
            thread.start();
        }
    }

    public void run() {
        Graphics g = getGraphics();
        while (running) {
            drawDisplay(g);
            
            if (loop > 99) {
                running = false;
            }
            try {
                Thread.sleep(speedGame);
            } catch (InterruptedException ie) {
            }
        }

        if (loop > 20) {
            /*
            if (getWidth() < getHeight()) {
            if (getHeight() > acuanDisable) {
            } else {
            midlet.showMenuCanvas();
            }
            } else {
            //midlet.showMenuCanvas(false);
            //midlet.showTypingCanvas640();
            midlet.showMenuCanvas();
            }
             * 
             */
            //System.out.println("mau ke menu");
            midlet.showMenuCanvas();
        }

    }

    protected void drawDisplay(Graphics g) {
        g.setColor(0xffccff);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0x0000ff);
        g.drawString("MoPet!!!", 80, 100, Graphics.TOP | Graphics.LEFT);
        loop++;
        flushGraphics();
    }

    /*
    private void dismiss() {
    if (isShown()) {
    host.showMenuCanvas();
    }
    }
     * 
     */
    protected void sizeChanged(int w, int h) {
        //super.sizeChanged(w, h);
    }

    protected void hideNotify() {
        //super.hideNotify();
        //hideNotify();
        paused();
        int xResume = getWidth() - imgBtnResume.getWidth();
        int yResume = getHeight() - imgBtnResume.getHeight();

        btnResume.setPositionXY(xResume, yResume,
                Graphics.TOP | Graphics.LEFT);
    }

    protected void showNotify() {
        //super.showNotify();
        //showNotify();
        if (isPaused) {
            Graphics g = getGraphics();
            btnResume.paint(g);
            //checkForKeys();
        }
    }

    private void paused() {
        isPaused = true;
        running = false;
    }

    protected void pointerPressed(int x, int y) {
        if (isPaused) {
            if (x > btnResume.xImage
                    && x < (btnResume.xImage + btnResume.imageWidth)
                    && y > btnResume.yImage
                    && y < (btnResume.yImage + btnResume.imageHeight)) {
                isPaused = false;
                start();
                repaint();
            }
        }
    }
}
